﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler
{
    public class UserRoledepCommendHandlers : IRequestHandler<UserRoledepCommends, APIResult<List<UserRoledepDto>>>
    {

        private readonly IBaseRepository<User> userRep;

        private readonly IBaseRepository<Role> roleRep;

        private readonly IBaseRepository<RoleUser> ruRep;
        private readonly IMapper mapper;

        public UserRoledepCommendHandlers(IBaseRepository<User> userRep, IBaseRepository<Role> roleRep, IBaseRepository<RoleUser> ruRep, IMapper mapper)
        {
            this.userRep = userRep;
            this.roleRep = roleRep;
            this.ruRep = ruRep;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取用户角色部门数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<UserRoledepDto>>> Handle(UserRoledepCommends request, CancellationToken cancellationToken)
        {
            APIResult<List<UserRoledepDto>> result = new APIResult<List<UserRoledepDto>>();
            var users=userRep.GetAllAsync().Result;
            var roles=roleRep.GetAllAsync().Result;
            var roleusers=ruRep.GetAllAsync().Result;
            var list = from a in users
                       join b in roleusers on a.UserId equals b.UserId
                       join c in roles on b.RoleId equals c.RoleId
                       select new UserRoledepDto
                       {
                           UserId = a.UserId,
                           UserName = a.UserName,
                           NickName = a.NickName,
                           DepId = a.DepId,
                           RoleId = c.RoleId,
                           RoleName = c.RoleName,
                       };
            result.data=list.ToList();
            return Task.FromResult(result);
        }
    }
}
